//
//  ReadViewController.m
//  product_B
//
//  Created by lanou on 16/5/26.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ReadViewController.h"
#import "ReadScrollViewModel.h"
#import "ScrollView.h"
#import "ReadScrollListViewController.h"
#import "ReadMainScrollView.h"
#import "ReadMainModel.h"
#import "ReadSmallScrollView.h"
#import "ReadDetailViewController.h"
#import "ReadEverListViewController.h"
@interface ReadViewController ()<ScorllDelegate,ReadSmallSrollViewDelegate,ReadScrollDelegate>
@property (nonatomic,strong)NSMutableArray *scrollViewModelArray;
@property (nonatomic,strong)ScrollView *scrollView;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)ReadMainScrollView *mainScrollView;
@property (nonatomic,strong)NSMutableArray *mainModelArray;
@property (nonatomic,strong)NSMutableArray *detailModelArray;
@end

@implementation ReadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title= @"阅读";
    self.titleLabel.text = @"阅读";
    self.scrollView = [[ScrollView alloc]initWithFrame:CGRectMake(0, 64, kWidth, kHeight / 675 * 180)];
    _scrollView.delegate = self;
    
    [self.view addSubview:_scrollView];
    _mainScrollView = [[ReadMainScrollView alloc]initWithFrame:CGRectMake(0, kHeight / 675 * 180 + 64, kWidth , kHeight - (kHeight / 675 * 180 + 64 + 40))];
    _mainScrollView.readScrollDelegate = self;
    [self.view addSubview:_mainScrollView];
    [self requestWork];
    
    
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.hidden = YES;
}
- (void)requestWork{
    [RequestManager initWithUrlString:@"http://v3.wufazhuce.com:8000/api/reading/carousel" requestType:@"GET" parDic:nil finishBlock:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.scrollViewModelArray = [ReadScrollViewModel readScrollViewModelWithConfigure:dic];
        [_scrollView setImageWithImageArray:self.scrollViewModelArray];
        
    } errorBlock:^(NSError *error) {
        
    }];
    [RequestManager initWithUrlString:@"http://v3.wufazhuce.com:8000/api/reading/index" requestType:@"GET" parDic:nil finishBlock:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.mainModelArray = [ReadMainModel readMainModelWithConfigure:dic];
        self.detailModelArray = [ReadMainModel readMainModelArrayWithConfigure:dic];
        [self.mainScrollView setReadMainModelWithArray:self.mainModelArray];
        for (ReadSmallScrollView *SmallView in _mainScrollView.smallScrollArray) {
            SmallView.readDelegate = self;
        }
        
    } errorBlock:^(NSError *error) {
        
    }];
}
- (void)clickedScrollViewWithIndex:(NSInteger)index{
    ReadScrollListViewController *readScrollListVC = [[ReadScrollListViewController alloc]init];
    readScrollListVC.model = self.scrollViewModelArray[index];
    CATransition *transition = [CATransition animation];
    transition.duration = 1.0f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = @"suckEffect";
    transition.subtype = kCATransitionFromBottom;
    transition.delegate = self;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    
    
    [self.navigationController pushViewController: readScrollListVC animated:YES];
}
- (void)clickScrollWithIndexX:(NSInteger)indexX andIndexY:(NSInteger)indexY{
    ReadDetailViewController *detailVC = [[ReadDetailViewController alloc]init];
    detailVC.modelArray = self.detailModelArray[indexY];
    detailVC.indexX = indexX;
    detailVC.indexY = indexY;
    [self.navigationController pushViewController:detailVC animated:YES];
}
- (void)pushReadEverController{
    ReadEverListViewController *everVC = [[ReadEverListViewController alloc]init];
    [self.navigationController pushViewController:everVC animated:YES];
}
- (void)scrollViewRequestWork{
    
    [RequestManager initWithUrlString:@"http://v3.wufazhuce.com:8000/api/reading/index" requestType:@"GET" parDic:nil finishBlock:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        self.mainModelArray = [ReadMainModel readMainModelWithConfigure:dic];
        self.detailModelArray = [ReadMainModel readMainModelArrayWithConfigure:dic];
        [self.mainScrollView setReadMainModelWithArray:self.mainModelArray];
        for (ReadSmallScrollView *SmallView in _mainScrollView.smallScrollArray) {
            SmallView.readDelegate = self;
        }
        
    } errorBlock:^(NSError *error) {
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
